
clear
set obs 1
gen blank =1
save ${nc_raw_data}/temp3, replace

forvalues t=2006/2013 {
	disp "************`t'***********"
	local f = "accdemopub`t'"
	if `t'>=2013 local f = "accdemo_pub`t'"
	use ${nc_raw_data}/`f' /* in 1/1000 */, clear



	gen schoolyr = `t'
	destring reporting_year days* , force replace
	capture destring aig, force replace
	capture noisily destring ndays_not_v10 ndays_v10 times* exc_abs unexc_abs , force replace

	foreach x in date_enter_hs withdraw_date registered_date {
		capture noisily {
			gen `x'0 = date(`x', "dmy")
			drop `x'
			rename `x'0 `x'
			drop `x'0
		}
	}
	tostring ethnic, force replace

	rename ethnic ethnic0
	gen ethnic = ethnic0
	replace ethnic = "A" if ethnic0=="2"
	replace ethnic = "B" if ethnic0=="4"
	replace ethnic = "H" if ethnic0=="3"
	replace ethnic = "I" if ethnic0=="1"
	replace ethnic = "M" if ethnic0=="6"
	replace ethnic = "W" if ethnic0=="5"

	append using ${nc_raw_data}/temp3
	save ${nc_raw_data}/temp3, replace
	tab schoolyr, m
}

drop if blank==1
drop blank
order mastid bdate schoolyr grade lea schlcode ethnic sex aig times* days* withdraw* registered* i_*
sort mastid schoolyr daysmem

gen g = real(grade)
replace g = 0 if grade=="0K"
replace g = -1 if grade=="PK"



save ${nc_clean_data}/accdemo2006_2013, replace

drop i_*
gen i_aig = aig>0
replace i_aig= . if aig==.

gen i_aigr = aig==1 | aig==2
replace i_aigr = . if aig==.

gen i_aigm = aig==1 | aig==3
replace i_aigm = . if aig==.

gen i_aigmr = aig==1
replace i_aigmr = . if aig==.

gen i_sm = (sex=="M")
gen i_bl = ethnic=="B"
gen i_wh = ethnic=="W"
gen i_hi = ethnic=="H"
gen i_as = ethnic=="A"
gen i_mr = ethnic=="M"

collapse (max) bdate i_* g (sum)  *_abs times* days* , by(mastid schoolyr)
save ${nc_clean_data}/accdemo, replace

collapse (max) bdate i_* max_g=g  max_sy=schoolyr (min) min_g=g  min_sy=schoolyr (mean) daysabs , by(mastid)
save ${nc_clean_data}/accdemo_1ob, replace

erase ${nc_raw_data}/temp3.dta
